package com.joelaws.prototype.client;

import com.gwtext.client.widgets.Panel;
import com.joelaws.prototype.client.data.User;
import com.joelaws.prototype.client.service.ServicePanel;

public class ScreenManager {

	private static ScreenManager manager;

	private Panel centerPanel;

	private TopPanel topPanel;

	public static ScreenManager instance() {
		return manager;
	}

	public static ScreenManager init(TopPanel topPanel, Panel centerPanel) {
		manager = new ScreenManager(topPanel, centerPanel);
		return manager;
	}

	private ScreenManager(TopPanel topPanel, Panel centerPanel) {
		this.topPanel = topPanel;
		this.centerPanel = centerPanel;
	}

	public void setLoggedIn(User user) {
		topPanel.setLoggedIn(user);
		showScreen(new ServicePanel(user));
	}

	public void setLoggedOut() {
		topPanel.setLoggedOut();
		showScreen(new LoginPanel());
	}

	public void showScreen(CenterPanel screen) {
		if (screen == null || screen.getName().equals("")) {
			System.out.println("Trying to render an invalid center panel");
		} else {
			System.out.println("Showing panel " + screen.getName());
			centerPanel.clear();
			centerPanel.add(screen);
			centerPanel.doLayout();
		}

	}
}